package com.zhht.checkcop.parser;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.log4j.Logger;

import com.zhht.checkcop.db.MysqlDb;
import com.zhht.checkcop.db.StaticDBMap;

import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

public class ParserMovieInfoByUpdateTime implements Pipeline{
	private final static Logger log= Logger.getLogger(ParserMovieInfoByUpdateTime.class);
	private String type;
	public ParserMovieInfoByUpdateTime(){}
	public ParserMovieInfoByUpdateTime(String type){
		this.type=type;
	}
	public void process(ResultItems resultItems, Task arg1) {
		Map<String, Object> mapResults = resultItems.getAll();
        Iterator<Entry<String, Object>> iter = mapResults.entrySet().iterator();
        Map.Entry<String, Object> entry;
        
        String infoUrl="";
        String updateTime=null;
        // 输出到控制台
        while (iter.hasNext()) {
            entry = iter.next();
            if(entry.getKey().equals("infoUrl")){
            	infoUrl = entry.getValue() + "";
            }
            if(entry.getKey().equals("updateTime")){
            	updateTime = entry.getValue() + "";
            }
        }
        
        try{
        	if(updateTime==null)
        		return;
        	
        	Map<String, Object> exitNovelMap = StaticDBMap.existNovelNameMap.get(infoUrl);
        	
        	//新增
        	execUpdateDB(updateTime,exitNovelMap.get("id")+"");
        	
        }catch(Exception e){
    		e.printStackTrace();
    	}
	}
	
	public void execUpdateDB(String updateTime,String id){
		MysqlDb mysqldb = null;
		try{
			mysqldb = new MysqlDb();
			mysqldb.connect();
			
			Map<String,Object> replace_value = new HashMap<String, Object>();
			replace_value.put("updateTime", updateTime);
			Map<String,Object> condition = new HashMap<String, Object>();
			condition.put("id", id);
			
			mysqldb.update("movie_info", replace_value, null, condition);
			
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try {
				mysqldb.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}
